/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

jsForms.Components["common.ListBox"]={
	Init:function()
	{
		this.P.UpdateCount=0;
		if (!this.P.SelectedClass)
		{
			this.P.SelectedClass="listbox-item-select";
		}
		if (!this.P.Items)
		{
			this.P.Items={};
		}
		this.bind('click',this,this.eventClick );
		return this;
	},/*
	BeginUpdate:function()
	{
		this.Properties.UpdateCount++;
	},
	EndUpdate:function()
	{
		this.Properties.UpdateCount--;
		if (this.Properties.UpdateCount<=0)
		{
			this.Update();
		}
	},*/
	eventClick : function(e)
	{
		if (!e.data) return;
			var el = e.target;
		
			do
			{
				var obj = jsForms.GetObject(el);
				if (obj) {
					switch (obj._Type) {
						case 'common.ListBoxItem':
							e.data.Select(obj);
							return;
						case 'common.ListBox':
							return;
						default:
							el = el.parentNode;
							break;
					}
				}
				else{
					el = el.parentNode;
				}
			}while(el != undefined);
	},
	RemoveItem:function(id)
	{
		var item = this.P.Items[id];
		if (item)
		{
			item.remove();
		}
		delete(this.P.Items[id]);
		return this;
	},
	Clear:function()
	{
		if (this.P.Items) {
			delete (this.P.Items);
		}
		this.C.content.empty();
	},
	AddItem:function(id,text)
	{
		var itemL = jsForms.CreateComponent('common.ListBoxItem');
		itemL.SetText(text);
		itemL.SetId(id);
		if (!this.P.Items)
		{
			this.P.Items={};
		}
		this.P.Items[id] = itemL;
		this.C.content.append(itemL);
		return this;
	},
	SetList:function(list)
	{
		for (var i in list) {
			var item = list[i];
			this.AddItem(item.id,item.text);
		}
		return this;
	},
	Select:function(item)
	{
		if (this.P.SelectedItem != item)
		{
			if (this.P.SelectedItem)
			{
				this.P.SelectedItem.removeClass(this.P.SelectedClass);
			}
			this.P.SelectedItem = this.P.Items[item.GetId()];
			if (this.P.SelectedItem)
			{
				this.P.SelectedItem.addClass(this.P.SelectedClass);
			}
			this._ExecEvent('onChangeSelect',this.P.SelectedItem);
		}
		return this;
	}
};

jsForms.Components["common.ListBoxItem"]={
	SetText:function(text)
	{
		this.C.text[0].innerHTML = text;
		return this;
	},
	GetText:function()
	{
		return this.C.text[0].innerHTML;
	},
	SetId:function(id)
	{
		if (this.P.id != id) {
			this.P.id = id;
			this[0].setAttribute('iid', id);
		}
		return this;
	},
	GetId:function()
	{
		return this.P.id;
	}
}

// -----------------------------------------------
// Compilation INFO
// -----------------------------------------------
jsForms.CreateCompileInfoByObject(
{
	Name: 'common.ListBox',
	Properties: {
		List: {
			Type: 'object',
			Access: 'SET'
		},
		SelectedClass: {
			Type: 'String',
			Access: 'direct'
		}
	},
	Events: {
		onChangeSelect: {}
	}
});
// -----------------------------------------------
// -----------------------------------------------
// Compilation INFO
// -----------------------------------------------
jsForms.CreateCompileInfoByObject(
{
	Name: 'common.ListBoxItem',
	Properties: {
		Id: {
			Access: 'GETSET'
		},
		Text: {
			Type: 'String',
			Access: 'getset'
		}
	},
	Events: {
	}
});
// -----------------------------------------------
